package cn.tuzirpa.config;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 数据库配置类
 * 支持配置数据库连接信息，包括URL、用户名、密码、库名和字符编码
 */
public class DatabaseConfig {
    
    @JsonProperty("url")
    private String url;
    
    @JsonProperty("username")
    private String username;
    
    @JsonProperty("password")
    private String password;
    
    @JsonProperty("databaseName")
    private String databaseName;
    
    @JsonProperty("charset")
    private String charset = "utf8mb4";
    
    @JsonProperty("collation")
    private String collation = "utf8mb4_unicode_ci";
    
    // 默认构造函数
    public DatabaseConfig() {}
    
    // 带参构造函数
    public DatabaseConfig(String url, String username, String password, String databaseName) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.databaseName = databaseName;
    }
    
    // 带字符编码的构造函数
    public DatabaseConfig(String url, String username, String password, String databaseName, String charset) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.databaseName = databaseName;
        this.charset = charset;
    }
    
    // Getter和Setter方法
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getDatabaseName() {
        return databaseName;
    }
    
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
    
    public String getCharset() {
        return charset;
    }
    
    public void setCharset(String charset) {
        this.charset = charset;
    }
    
    public String getCollation() {
        return collation;
    }
    
    public void setCollation(String collation) {
        this.collation = collation;
    }
    
    /**
     * 获取完整的数据库连接URL（包含数据库名）
     * 注意：此方法假设数据库已经存在，如果数据库不存在会导致连接失败
     */
    public String getFullUrl() {
        if (url == null || url.isEmpty()) {
            throw new IllegalArgumentException("数据库URL不能为空");
        }
        
        // 如果URL已经包含数据库名，直接返回
        if (url.contains("/" + databaseName + "?")) {
            return url;
        }
        
        // 构建包含数据库名的URL
        StringBuilder fullUrl = new StringBuilder(url);
        if (!url.endsWith("/")) {
            fullUrl.append("/");
        }
        fullUrl.append(databaseName);
        
        // 添加字符编码参数
        if (!url.contains("?")) {
            fullUrl.append("?");
        } else {
            fullUrl.append("&");
        }
        fullUrl.append("useUnicode=true&characterEncoding=UTF-8");
        fullUrl.append("&serverTimezone=Asia/Shanghai");
        
        return fullUrl.toString();
    }
    
    /**
     * 获取不包含数据库名的连接URL（用于创建数据库）
     */
    public String getBaseUrl() {
        if (url == null || url.isEmpty()) {
            throw new IllegalArgumentException("数据库URL不能为空");
        }
        
        // 移除数据库名部分
        String baseUrl = url;
        if (baseUrl.contains("/" + databaseName)) {
            baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf("/" + databaseName));
        }
        
        // 添加字符编码参数
        if (!baseUrl.contains("?")) {
            baseUrl += "?";
        } else {
            baseUrl += "&";
        }
        baseUrl += "useUnicode=true&characterEncoding=UTF-8";
        baseUrl += "&serverTimezone=Asia/Shanghai";
        
        return baseUrl;
    }
    
    @Override
    public String toString() {
        return "DatabaseConfig{" +
                "url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='***'" +
                ", databaseName='" + databaseName + '\'' +
                ", charset='" + charset + '\'' +
                ", collation='" + collation + '\'' +
                '}';
    }
}
